package com.jinmei.web.action;

import org.apache.commons.lang.StringUtils;

import com.jinmei.model.User;
import com.jinmei.security.crypto.PasswordManager;
import com.jinmei.service.UserService;
import com.jinmei.utils.JinmeiConsts;
import com.opensymphony.xwork2.ActionContext;

public class AdminLoginAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private UserService userService;

	private String password;
	private String username;
	private String workDate;
	
	public String login() throws Exception {
		User user = getUserService().findUserByName(username);
		PasswordManager pm = PasswordManager.getInstance();
		if (user == null) {
			request.setAttribute("msg", "用户名错误!");
			return "pwdError";
		} else {
			if (StringUtils.equals(password, pm.decrypt(user.getPassword()))) {
				// store user info into session
				user.setSessionid(request.getSession().getId());
				ActionContext.getContext().getSession().put(JinmeiConsts.USER, user);
				
				// send info to result page
				setUserSessionIdAndUsername();
				
				return SUCCESS;
			} else {
				request.setAttribute("msg", "密码错误!");
				return "pwdError";
			}
		}
	}
	
	public String logout() throws Exception {
		ActionContext.getContext().getSession().remove(JinmeiConsts.USER);
		
		return SUCCESS;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getWorkDate() {
		return workDate;
	}

	public void setWorkDate(String workDate) {
		this.workDate = workDate;
	}

}
